import { preloadAssetInBundle } from "../utils/util";

/**
 * 
 * 弹窗管理器
 * 
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class ToastManager {

    /**
     * 加载对应bundle里面的预制体
     * @param  {string} prefabUrl 预制体路径
     * @param  {string='resources'} bundleName bundle名字
     * @returns Promise
     */
    public async loadToastPrefab(prefabUrl: string,bundleName: string = 'resources'): Promise<cc.Prefab> {
        const prefab = await preloadAssetInBundle(prefabUrl,cc.Prefab,bundleName) as cc.Prefab;
        if(!prefab) return null;
        return prefab;
    }

    /**
     * 直接将预制体加入到父节点上
     * @param  {string} prefabUrl 预制资源url
     * @param  {cc.Node} parentNode 父节点
     * @param  {string='resource'} bundleName bundle名字
     * @returns Promise
     */
    public async addPrefabToParent(prefabUrl: string,parentNode: cc.Node,bundleName: string = 'resource'): Promise<void> {
        const prefab = await this.loadToastPrefab(prefabUrl,bundleName) as cc.Prefab;
        const prefabNode = cc.instantiate(prefab);
        if(prefabNode) {
            parentNode.addChild(prefabNode);
            prefabNode.setPosition(cc.v2(0,0));
        }
    }
}

export const toastManager = new ToastManager();
